package com.lingshi.shopping_goods_service.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping_common.entity.*;
import com.lingshi.shopping_common.service.IGoodsService;
import com.lingshi.shopping_goods_service.mapper.GoodsImageMapper;
import com.lingshi.shopping_goods_service.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@DubboService
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsImageMapper goodsImageMapper;

    @Override
    public void add(Goods goods) {
        // 保存商品数据
        goodsMapper.insert(goods);

        // 保存图片数据
        Long goodsId = goods.getId();
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goodsId);
            goodsImageMapper.insert(image);
        }

        // 插入商品_规格项数据
        // 1.获取规格
        List<Specification> specifications = goods.getSpecifications();
        // 2.获取规格项
        List<Specification> options = new ArrayList<>();
        // 遍历规格，获取规格中的所有规格项
        for (Specification specification : specifications) {
            List<SpecificationOption> specificationOptions = specification.getSpecificationOptions();
            options.addAll(specifications);
        }
        // 3.遍历规格项，插入商品_规格项数据
        for (Specification option : options) {
            goodsMapper.addGoodsSpecificationOption(goods.getId(),option.getId());
        }
    }

    @Override
    public void update(Goods goods) {
        // 删除旧图片数据
        Long goodsId = goods.getId(); // 商品id
        QueryWrapper<GoodsImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goodsId",goodsId);
        goodsImageMapper.delete(queryWrapper);
        // 删除旧规格项数据
        goodsMapper.deleteGoodsSpecificationOption(goodsId);

        // 保存商品数据
        goodsMapper.updateById(goods);
        // 保存图片数据
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goodsId);
            goodsImageMapper.insert(image);
        }

        // 插入商品_规格项数据
        // 1.获取规格
        List<Specification> specifications = goods.getSpecifications();
        // 2.获取规格项
        List<SpecificationOption> options = new ArrayList<>();
        // 遍历规格，获取规格中的所有规格项
        for (Specification specification : specifications) {
            System.out.println("specification = " + specification);
            List<SpecificationOption> specificationOptions = specification.getSpecificationOptions();
            options.addAll(specificationOptions);
        }
        // 3.遍历规格项，插入商品_规格项数据
        for (SpecificationOption option : options) {
            System.out.println("option = " + option);
            goodsMapper.addGoodsSpecificationOption(goods.getId(),option.getId());
        }
    }

    @Override
    public Goods findById(Long id) {
        return goodsMapper.findById(id);
    }

    @Override
    public void putAway(Long id, Boolean isMarketable) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setIsMarketable(isMarketable);
        goodsMapper.updateById(goods);
    }

    @Override
    public Page<Goods> search(Goods goods, int page, int size) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(goods) && StrUtil.isNotBlank(goods.getGoodsName())){
            queryWrapper.like(Goods::getGoodsName,goods.getGoodsName());
            queryWrapper.or();
            queryWrapper.like(Goods::getCaption,goods.getGoodsName());
        }
        return goodsMapper.selectPage(new Page<>(page,size),queryWrapper);
    }

    @Override
    public List<GoodsDesc> findAll() {
        return goodsMapper.findAll();
    }
}
